package com.example.day1.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.day1.DTO.DishDto;
import com.example.day1.DTO.SetmealDto;
import com.example.day1.common.CustomException;
import com.example.day1.common.R;
import com.example.day1.entity.Category;
import com.example.day1.entity.Dish;
import com.example.day1.entity.Setmeal;
import com.example.day1.service.CategoryService;
import com.example.day1.service.SetmealDishService;
import com.example.day1.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SeatmealController {
        @Autowired
    public SetmealDishService setmealDishService;

    @Autowired
    public SetmealService setmealService;
    @Autowired
    public CategoryService categoryService;


    @PostMapping
    public R<String> save(@RequestBody SetmealDto setMealDto){
        setmealService.saveWithDish(setMealDto);
        return R.success("新增信息成功");
    }
    @GetMapping("/page")
    public R<Page>  page(int page,int pageSize,String name){
        Page<Setmeal> objectPage = new Page<>(page,pageSize);
        Page<SetmealDto> objectPage1 = new Page<>();
        LambdaQueryWrapper<Setmeal> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.like(name!=null,Setmeal::getName,name);
        objectLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
            setmealService.page(objectPage,objectLambdaQueryWrapper);
        //由于返回值的类型不满足要求，所以就改变返回类型
        //复制将以由的dish属性复制给dto
        BeanUtils.copyProperties(objectPage,objectPage1,"records");
        List<Setmeal> records = objectPage.getRecords();
        List<SetmealDto> list= records.stream().map((item)->{
            SetmealDto seatmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,seatmealDto);
            Long categoryId = item.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            if(byId!=null){
                String name1 = byId.getName();
                seatmealDto.setCategoryName(name1);
            }
            return seatmealDto;
        }).collect(Collectors.toList());

        objectPage1.setRecords(list);
       return R.success(objectPage1);
    }

    @DeleteMapping()
    public void remove(@RequestParam List<Long> ids) throws CustomException {
        setmealService.removeWithDish(ids);
    }
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,1);
        objectLambdaQueryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        List<Setmeal> list = setmealService.list(objectLambdaQueryWrapper);
        return R.success(list);
    }

}
